<?PHP 
require("../public/common.php");
require("../config/connect.php");

$id = CheckUrlParamNoDie("id");
if ($id=="")
{
	echo "code=0";
	exit;	
}

//
$sql = "select num_iid,price,commission,commission_rate,click_url from tk_cache where num_iid='$id' and datediff(now(), time)<15 limit 1";
$result = mysql_query( $sql );
if ($result && mysql_num_rows($result) > 0)
{
	$row = mysql_fetch_assoc( $result );
	$click_url = $row["click_url"];
	$price=$row["price"];
	$commission=$row["commission"];
	$commission_rate=$row["commission_rate"];
}
else
{
	date_default_timezone_set("Asia/Chongqing");
	require("../admin/taobaosdk/TopSdk.php");
	$json = json_decode(GetItemInfo($id));
	if ($json->code == 1)
	{
		$click_url=$json->click_url;
		$commission=$json->commission;
		$commission_rate=$json->commission_rate;
		$price=$json->price;
		$sql = "insert into tk_cache (num_iid, price, commission, commission_rate, click_url) values ('$id', $price, $commission, $commission_rate, '$click_url') ON DUPLICATE KEY UPDATE price=$price, commission=$commission, commission_rate=$commission_rate, click_url='$click_url',time=now()";
//		echo $sql;
		mysql_query( $sql );
	}
}

if (isset($click_url))
	echo "1\r$price\r$commission_rate\r$click_url";
else
	echo "0";

//
function GetItemInfo($itemid)
{
	$c = new TopClient;
	$c->appkey = "12322390";
	$c->secretKey = "33184e84dfc3760bf4b8d7c80474bf6c";
	$req = new TaobaokeItemsConvertRequest;
	$req->setFields("click_url,num_iid,commission,commission_rate,price");
	$req->setNumIids($itemid);
	$req->setPid(17996058);
	$resp2 = $c->execute($req);
	
	if ($resp2->getName() == "taobaoke_items_convert_response" && isset($resp2->taobaoke_items))
	{
		$str = "{\"code\":1";
		$str .= ", \"click_url\":\"".$resp2->taobaoke_items[0]->taobaoke_item[0]->click_url."\"";
		$str .= ", \"price\":\"".$resp2->taobaoke_items[0]->taobaoke_item[0]->price."\"";
		$str .= ", \"commission\":\"".$resp2->taobaoke_items[0]->taobaoke_item[0]->commission."\"";
		$str .= ", \"commission_rate\":\"".$resp2->taobaoke_items[0]->taobaoke_item[0]->commission_rate."\"";
		$str .= "}";
		return $str;
	}
			
	return "{\"code\":0}";
}
?>